package cate.game.play.skill.passive.hero;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDSkill;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.fighter.HeroFighter;
import cate.game.play.proce.move.AppendMove;
import cate.game.play.proce.move.AppendMoveHandler;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.proce.stat.StatItem;
import cate.game.play.proce.turn.TurnCtx;
import cate.game.play.skill.Skill;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 友方单位死亡时，若未立即复活，对击杀者发射审判之箭，造成188%物理伤害;
 * 每使用一次审判之箭提升18%技能系数，至多提升72%技能系数
 */
public class 审判之箭PH extends PassiveHandler implements AppendMoveHandler {

	private int 追加技能;

	private double 系数;

	private double 最大系数;

	//追加技能=？？&系数=1800&最大系数=7200
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		追加技能 = args.getInt("追加技能", 0);
		系数 = args.getDouble("系数", 0d);
		最大系数 = args.getDouble("最大系数", 0d);
	}

	//当前提升的系数
	private double coeff;

	@Override
	public void afterMoveDone(MoveCtx move) {
		if (skill.owner.isDead()) {
			return;
		}
		Fighter actor = move.getActor();
		if (actor.isDead()) {
			return;
		}
		if (actor.getTeam() == skill.owner.getTeam()) {
			return;
		}
		if (actor.type != GDFight.Fighter.HERO) {
			return;
		}
		for (StatItem stat : move.getStat().findStats(s -> s.actorPid == actor.getPid() && s.type == GDSkill.StatType.KILL)) {
			HeroFighter target = skill.owner.getFightTeam().findHeroByPid(stat.targetPid);
			if (target == null || target.isAlive()) {
				continue;
			}
			addAppendMove(move, move.getActor());
		}
	}

	public void addAppendMove(MoveCtx move, Fighter target) {
		Skill appendSkill = move.getCfg().skill.buildById(skill.owner, 追加技能);
		if (appendSkill == null) {
			return;
		}
		appendSkill.param.addAppointTarget(target);
		move.getTurn().addAppend(new AppendMove(skill.owner, appendSkill, this));
	}

	@Override
	public boolean inValid(TurnCtx turn, AppendMove appendMove) {
		return false;
	}

	@Override
	public void beforeAppendMove(TurnCtx turn, AppendMove appendMove) {
		if (coeff > 0) {
			appendMove.skill.param.addHpCfgByCoeff(coeff);
		}
	}

	@Override
	public void afterAppendMove(TurnCtx turn, AppendMove appendMove) {
		if (coeff < 最大系数) {
			coeff += 系数;
			coeff = Math.min(coeff, 最大系数);
		}
	}

	@Override
	public void onAppendMoveFail(TurnCtx turn, AppendMove appendMove) {

	}
}
